/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import exceptions.DataSourceException;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modele.InterfaceClient;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Jean-Michel
 */
public class ClientControler extends GenericAction {
    
        public ActionForward clientListe(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException, DataSourceException {

        ActionMessages errors = new ActionMessages();

        // Redirection attendue si tout se passe bien
        String target = new String("success");

        // Teste si l'utilisateur est identifie
//        HttpSession session = request.getSession();
//        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas loggué
//            target = new String("login");

//            errors.add(ActionMessages.GLOBAL_MESSAGE,
//                    new ActionMessage("errors.login.required"));

            // Signalement des erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }

            // Si jamais l'utilisateur n'est pas loggé, alors
            // on retourne directement à la page de login
            // avant d'effectuer la moindre requête
//            return (mapping.findForward(target));
//        }

        ArrayList clts;

        try {

//            InterfaceEmploye employeDB = (EmployeDB) getModele(request);
            InterfaceClient employeDB = (InterfaceClient) getModele();
            clts = (ArrayList) employeDB.getAllClients();
            request.setAttribute("clients", clts);

        } catch (NullPointerException e) {
            throw new DataSourceException();
        } catch (DataSourceException e) {

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.database.error"));
//            target = new String("login");
        }

        // Signalement des erreurs eventuelles
        if (!errors.isEmpty()) {
            saveErrors(request, errors);
        }

        // Forward to the appropriate View    
        return (mapping.findForward(target));

    }
}
